<?php
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

class DocarioViewEditfile extends JView {
    var $_msg = '';
    var $_file = null;
    var $_file_name = '';
    
    // edit form modes
    const EDIT_FILE = 1;
    const NEW_FILE = 0; // default mode
    
    var $_mode = DocarioViewEditfile::NEW_FILE;
    
    function setMsg($msg) {
        $this->_msg = $msg;
    }
    
    function setMode($mode) { $this->_mode = $mode; }
    
    function setFile($file_name) {
        $this->_file_name = $file_name;
    }
    
    function display($tpl = null) {
        $doc = JFactory::getDocument();
        $doc->addScript('components'.DS.'com_docario'.DS.'assets'.DS.'javascript'.DS.'fileform.js');
        $doc->addStylesheet('components'.DS.'com_docario'.DS.'assets'.DS.'css'.DS.'docario.css');
    
        JToolBarHelper::title(JText::_('EDIT_FILE'));
        JToolBarHelper::save('saveFile');
        JToolBarHelper::cancel();
        
        $cid = JRequest::getVar('catid');
        $id = JRequest::getVar('id');
        
        $model = $this->getModel();
        $model->setId($id);
        $model->setCatId($cid);
        
        $this->_file = $model->getFileTmp($this->_file_name);
        
        if ($this->_mode == DocarioViewEditfile::EDIT_FILE) {
            $this->_file = $model->getFile();
            $file = $this->_file;
            $download_filename = $this->_file->download_filename;
            $mime_type = $this->_file->mime_type;
        } else {
            $download_filename = $this->_file->name;
            $mime_type = $this->_file->mime_type;
            $file = $model->getFile();
        }
        
        if ($file->userid > 0) $owner = JFactory::getUser($file->userid);
        else $owner = JFactory::getUser();
        
        if ($file->id > 0) { // file exists
            $file_users = $model->getFileUserPermissions();
            foreach ($file_users as &$user) {
                if ($user->read == 0) $user->css_read = 'upermin';
                else if ($user->read == 1) $user->css_read = 'upermon';
                else if ($user->read == 2) $user->css_read = 'upermoff';
                
                if ($user->write == 0) $user->css_write = 'upermin';
                else if ($user->write == 1) $user->css_write = 'upermon';
                else if ($user->write == 2) $user->css_write = 'upermoff';
            }
        } else {
            $file_users = $model->getUsers();
            foreach ($file_users as &$user) {
                $user->read = 0; $user->css_read = 'upermin';
                $user->write = 0; $user->css_write= 'upermin';
            }
        }
        $this->assignRef('file_users', $file_users);
        
        $this->_file->mime_type = 'default'; // temporal solution (css-mime)
        
        $category = $model->getCategory();
        $categories = $model->getAllCategories();
        
        $this->assignRef('file', $file);
        $this->assignRef('file_tmp', $this->_file);
        $this->assignRef('category', $category);
        $this->assignRef('cid', $cid);
        $this->assignRef('id', $id);
        $this->assignRef('owner', $owner);
        $this->assignRef('categories', $categories);
        $this->assignRef('msg', $this->_msg);
        $this->assignRef('download_filename', $download_filename);
        $this->assignRef('mime_type', $mime_type);
        
        parent::display($tpl);
    }
}
